package com.eistert.java.datastructure._02_Stack;

import com.eistert.java.datastructure._01_Array._01_Array;

/**
 * first in last out
 *
 * @Description: stack
 * @Author: ai
 * @create: 2023-04-04 09:09
 */
public class _02_Array02Stack<E> implements _02_Stack<E> {

    private _01_Array<E> array;


    public _02_Array02Stack(int capacity) {
        array = new _01_Array<>(capacity);
    }

    public _02_Array02Stack() {
        array = new _01_Array<>();
    }


    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public void push(E e) {
        array.addLast(e);

    }

    @Override
    public E pop() {
        return array.removeLast();
    }

    @Override
    public E peek() {
        return array.getLast();
    }


    /**
     * print the stack on the console
     *
     * @return
     */
    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder();
        ret.append("Stack: ");
        ret.append('[');
        for (int i = 0; i < array.getSize(); i++) {
            ret.append(array.get(i));
            if (i != array.getSize() - 1) {
                ret.append(", ");
            }
        }

        ret.append("] top");

        return ret.toString();
    }


    /**
     * test my _02_ArrayStack and _01_Array
     *
     * @param args
     */
    public static void main(String[] args) {

        _02_Array02Stack<Integer> stack = new _02_Array02Stack<>();

        for (int i = 0; i < 5; i++) {
            stack.push(i);
            System.out.println(stack);
        }

        stack.pop();
        System.out.println(stack);
    }
}
